/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.system.common.page;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author value
 * @Date 2019年1月25日22:07:36
 */
@Data
@NoArgsConstructor
public class PageInfo<T> {

    private long total;

    private Integer code = 0;

    private String msg = "请求成功";

    private List<T> rows;

    /**
     * 分页
     *
     * @param list  列表数据
     * @param total 总记录数
     */
    public PageInfo(List<T> list, long total) {
        this.rows = list;
        this.total = total;
    }

    public PageInfo(long total, @NotNull List<T> list) {
        this.total = total;
        this.rows = list;
    }

    /**
     * 分页返回对象
     *
     * @param page page
     * @param <T>  <T>
     * @return <T>
     */
    public static <T> PageInfo<T> build(IPage<T> page) {
        PageInfo<T> rspData = new PageInfo<>();
        rspData.setCode(HttpStatus.HTTP_OK);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    /**
     * 分页返回对象
     *
     * @param list list
     * @param <T>  <T>
     * @return <T>
     */
    public static <T> PageInfo<T> build(List<T> list) {
        PageInfo<T> rspData = new PageInfo<>();
        rspData.setCode(HttpStatus.HTTP_OK);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }

    /**
     * 分页返回对象
     *
     * @param <T> <T>
     * @return <T>
     */
    public static <T> PageInfo<T> build() {
        PageInfo<T> rspData = new PageInfo<>();
        rspData.setCode(HttpStatus.HTTP_OK);
        rspData.setMsg("查询成功");
        return rspData;
    }

}
